package cn.edu.swu.book;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

public class BookService {

    // 单例模式 singleton

    private final BookRepo repo;
    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");

    private static final BookService instance = new BookService();

    private BookService(){
        this.repo = new BookRepo();
    }

    public static BookService getInstance() {
        return instance;
    }

    public List<Book> getAll() throws SQLException, ClassNotFoundException {
        return this.repo.getAllBooks();
    }

    public void save(String name, String author, double price, String description, String publish) throws SQLException {
        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(price);
        book.setMemo(description);
        try {
            book.setPublish(simpleDateFormat.parse(publish));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.repo.save(book);
    }

    public void delete(int bookId) throws SQLException {
        this.repo.delete(bookId);
    }

    public Book getBookById(int id) throws SQLException {
        return this.repo.getBookById(id);
    }

    public void updateBook(int id, String name, String author, double price, String description, String publish) throws SQLException {
        Book book = new Book();
        book.setId(id);
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(price);
        book.setMemo(description);
        try {
            book.setPublish(simpleDateFormat.parse(publish));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.repo.updateBook(book);
    }

    public List<Book> search(String key) throws SQLException {
        return this.repo.search(key);
    }

    public String wrapTable(List<Book> books) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table class='bs'>\n");
        sb.append("<tr><th>编号</th><th>书名</th><th>作者</th><th>价格</th><th>描述</th><th>发行日期</th><th></th><th></th></tr>\n");
        for (Book book : books) {
            sb.append("<tr>");
            sb.append("<td class='center'>").append(book.getId()).append("</td>");
            sb.append("<td>").append(book.getName()).append("</td>");
            sb.append("<td>").append(book.getAuthor()).append("</td>");
            sb.append("<td class='center'>").append(book.getPrice()).append("</td>");
            sb.append("<td>").append(book.getMemo()).append("</td>");
            sb.append("<td class='center'>").append(book.getPublish()).append("</td>");
            sb.append("<td class='center'><a href='./delete?id=" + book.getId() + "'>删除</a></td>");
            sb.append("<td class='center'><a href='./edit?id=" + book.getId() + "'>修改</a></td>");
            sb.append("</tr>");
        }
        sb.append("</table>");
        return sb.toString();
    }

    public String wrapForm(Book book) {
        StringBuilder sb = new StringBuilder();
        sb.append("<center><br>")
            .append("<form action=\"./update\" method=\"post\">")
            .append("<input type=\"hidden\" name=\"id\" value='" + book.getId() + "'><br><br>")
            .append("书名：<input type=\"text\" name=\"name\" value='" + book.getName() + "'><br><br>")
            .append("作者：<input type=\"text\" name=\"author\" value='" + book.getAuthor() + "'><br><br>")
            .append("价格：<input type=\"text\" name=\"price\" value='" + book.getPrice() + "'><br><br>")
            .append("备注：<input type=\"text\" name=\"description\" value='" + book.getMemo() + "'><br><br>")
            .append("日期：<input type=\"text\" name=\"publish\" value='" + book.getPublish() + "'><br><br>")
            .append("<input type=\"submit\" name=\"submit\" value=\"修改图书\"/>")
            .append("</form></center>");

        return sb.toString();
    }
}
